package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.dao.Area_banco;
import br.com.evl.tecinfo.dao.Cargo_Banco;
import br.com.evl.tecinfo.dao.FormaDePagamento_Banco;
import br.com.evl.tecinfo.dao.Funcionario_Banco;
import br.com.evl.tecinfo.modelo.Area;
import br.com.evl.tecinfo.modelo.Cargo;
import br.com.evl.tecinfo.modelo.FormaDePagamento;
import br.com.evl.tecinfo.modelo.Funcionario;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Tecinfo
 */
public class TelaCadastroFuncionario extends javax.swing.JFrame {

    /**
     * Creates new form TelaCadastroFuncionario
     */
    private FormaDePagamento_Banco pagamentoBD;
    private Cargo_Banco cargoBD;
    private Area_banco areaBD;
    private Funcionario_Banco funcionarioBD;

    private Funcionario novoFuncionario;
    private FormaDePagamento pagamento;
    private Cargo cargo;
    private Area area;

    public TelaGeral telaGeral;
    int editar = 0;

    public TelaCadastroFuncionario() {
        initComponents();

        novoFuncionario = new Funcionario();

        listarFormaPagamento();
        listarCargos();
        listarAreas();
        atualizarTabela();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        JT_Table = new javax.swing.JTable();
        jLB_nome = new javax.swing.JLabel();
        jLB_cargo = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLB_area = new javax.swing.JLabel();
        jLB_valorArea = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jB_remover = new javax.swing.JButton();
        jB_editar1 = new javax.swing.JButton();
        jBcadastrar_tc1 = new javax.swing.JButton();
        JB_voltar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jcbFormasPagamentos = new javax.swing.JComboBox();
        jtfAgencia = new javax.swing.JTextField();
        jtfConta = new javax.swing.JTextField();
        jT_nome = new javax.swing.JTextField();
        jTvalorhr_tc = new javax.swing.JTextField();
        jTF_cpf = new javax.swing.JTextField();
        jtfBanco = new javax.swing.JTextField();
        jcbAreas = new javax.swing.JComboBox();
        jcbCargos = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        Jback = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar funcionário");
        setMinimumSize(new java.awt.Dimension(600, 600));
        setResizable(false);
        getContentPane().setLayout(null);

        JT_Table.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JT_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Cargo", "Forma de Pag.", "CPF", "Área", "Valor da Hora", "Conta", "Ag", "Banco"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(JT_Table);
        if (JT_Table.getColumnModel().getColumnCount() > 0) {
            JT_Table.getColumnModel().getColumn(2).setPreferredWidth(130);
            JT_Table.getColumnModel().getColumn(5).setPreferredWidth(100);
        }

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 330, 580, 210);

        jLB_nome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLB_nome.setText("Nome:");
        getContentPane().add(jLB_nome);
        jLB_nome.setBounds(50, 120, 40, 15);

        jLB_cargo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLB_cargo.setText("Cargo:");
        getContentPane().add(jLB_cargo);
        jLB_cargo.setBounds(50, 280, 41, 15);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Pagamento");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(390, 280, 20, 15);

        jLB_area.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLB_area.setText("Área:");
        getContentPane().add(jLB_area);
        jLB_area.setBounds(230, 280, 33, 15);

        jLB_valorArea.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLB_valorArea.setText("Valor da Hora:");
        getContentPane().add(jLB_valorArea);
        jLB_valorArea.setBounds(50, 160, 90, 15);

        jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 30)); // NOI18N
        jLabel2.setText("Cadastro Funcionário");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(190, 30, 310, 36);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("CPF:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(50, 200, 27, 20);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("CC:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(230, 240, 20, 15);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("AG:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(50, 240, 20, 15);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/logomarcav2.png"))); // NOI18N
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 0, 140, 110);

        jB_remover.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_remover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_excluirv2.png"))); // NOI18N
        jB_remover.setText("Remover");
        jB_remover.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jB_remover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_removerActionPerformed(evt);
            }
        });
        getContentPane().add(jB_remover);
        jB_remover.setBounds(370, 550, 120, 40);

        jB_editar1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_editar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icon_editar1v2.png"))); // NOI18N
        jB_editar1.setText("Editar");
        jB_editar1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jB_editar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_editar1ActionPerformed(evt);
            }
        });
        getContentPane().add(jB_editar1);
        jB_editar1.setBounds(240, 550, 120, 40);

        jBcadastrar_tc1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBcadastrar_tc1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_add1v2.png"))); // NOI18N
        jBcadastrar_tc1.setText("Cadastrar");
        jBcadastrar_tc1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jBcadastrar_tc1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcadastrar_tc1ActionPerformed(evt);
            }
        });
        getContentPane().add(jBcadastrar_tc1);
        jBcadastrar_tc1.setBounds(110, 550, 120, 40);

        JB_voltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/voltar.png"))); // NOI18N
        JB_voltar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        JB_voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_voltarActionPerformed(evt);
            }
        });
        getContentPane().add(JB_voltar);
        JB_voltar.setBounds(540, 10, 50, 40);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setText("Banco:");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(400, 240, 43, 15);

        jcbFormasPagamentos.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        getContentPane().add(jcbFormasPagamentos);
        jcbFormasPagamentos.setBounds(450, 280, 100, 24);

        jtfAgencia.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jtfAgencia.setText("000");
        jtfAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfAgenciaActionPerformed(evt);
            }
        });
        getContentPane().add(jtfAgencia);
        jtfAgencia.setBounds(450, 230, 100, 30);

        jtfConta.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jtfConta.setText("0000");
        getContentPane().add(jtfConta);
        jtfConta.setBounds(100, 230, 100, 30);

        jT_nome.setText("nome");
        jT_nome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jT_nomeActionPerformed(evt);
            }
        });
        getContentPane().add(jT_nome);
        jT_nome.setBounds(180, 110, 370, 30);

        jTvalorhr_tc.setText("00,00");
        jTvalorhr_tc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTvalorhr_tcActionPerformed(evt);
            }
        });
        getContentPane().add(jTvalorhr_tc);
        jTvalorhr_tc.setBounds(180, 150, 370, 30);

        jTF_cpf.setText("000.000.000-00");
        getContentPane().add(jTF_cpf);
        jTF_cpf.setBounds(180, 190, 370, 30);

        jtfBanco.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jtfBanco.setText("0000000-0");
        getContentPane().add(jtfBanco);
        jtfBanco.setBounds(270, 230, 100, 30);

        jcbAreas.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        getContentPane().add(jcbAreas);
        jcbAreas.setBounds(270, 280, 100, 24);

        jcbCargos.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        getContentPane().add(jcbCargos);
        jcbCargos.setBounds(100, 280, 100, 24);

        jLabel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(jLabel4);
        jLabel4.setBounds(10, 100, 580, 210);

        Jback.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/back_azul.jpg"))); // NOI18N
        Jback.setText("jLabel3");
        Jback.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        Jback.setMaximumSize(new java.awt.Dimension(600, 600));
        Jback.setMinimumSize(new java.awt.Dimension(600, 600));
        getContentPane().add(Jback);
        Jback.setBounds(0, 0, 600, 600);

        setSize(new java.awt.Dimension(616, 638));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jTvalorhr_tcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTvalorhr_tcActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTvalorhr_tcActionPerformed

    private void jT_nomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jT_nomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jT_nomeActionPerformed

    private void jB_removerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_removerActionPerformed

        novoFuncionario = new Funcionario();

        novoFuncionario.setCpf((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 3));

        funcionarioBD.remover(novoFuncionario);

        atualizarTabela();

    }//GEN-LAST:event_jB_removerActionPerformed

    private void jB_editar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_editar1ActionPerformed

        if (editar == 0) {

            jT_nome.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 0));
            jcbCargos.setSelectedItem((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 1));
            jcbFormasPagamentos.setSelectedItem((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 2));
            jTF_cpf.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 3));
            jcbAreas.setSelectedItem((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 4));
            jTvalorhr_tc.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 5));
            jtfConta.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 6));
            jtfAgencia.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 7));
            jtfBanco.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 8));
            jB_editar1.setText("Concluir");
            jB_remover.setEnabled(false);
            jBcadastrar_tc1.setEnabled(false);
            JT_Table.setEnabled(false);
            editar = 1;

        } else {

            jB_removerActionPerformed(evt);
            jBcadastrar_tc1ActionPerformed(evt);

            jB_editar1.setText("Editar");
            jB_remover.setEnabled(true);
            jBcadastrar_tc1.setEnabled(true);
            JT_Table.setEnabled(true);
            editar = 0;

        }

    }//GEN-LAST:event_jB_editar1ActionPerformed

    private void jBcadastrar_tc1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcadastrar_tc1ActionPerformed

        novoFuncionario = new Funcionario();

//novo cargo
        cargo = new Cargo();

        Object cargoSelecionado = jcbCargos.getSelectedItem();

        cargoBD = new Cargo_Banco();

        try {
            while (cargoBD.consulta.next()) {

                if (cargoBD.consulta.getString("nome").equals(cargoSelecionado)) {
                    cargo.setNome(cargoBD.consulta.getString("nome"));
                }
            }
        } catch (SQLException ex) {

            //JOptionPane.showMessageDialog(null, "erro ao criar cargo");
            System.out.println("erro ao criar objeto cargo");
        }
//novo pagamento
        pagamento = new FormaDePagamento();

        Object pagamentoSelecionado = jcbFormasPagamentos.getSelectedItem();

        pagamentoBD = new FormaDePagamento_Banco();

        try {
            while (pagamentoBD.consulta.next()) {

                if (pagamentoBD.consulta.getString("nome").equals(pagamentoSelecionado)) {
                    pagamento.setNome(pagamentoBD.consulta.getString("nome"));
                }
            }
        } catch (SQLException ex) {
            //JOptionPane.showMessageDialog(null, "erro ao criar forma de pagamento");

            System.out.println("erro ao criar objeto Pagamento");
        }

//nova area
        area = new Area();

        Object areaSelecionada = jcbAreas.getSelectedItem();

        areaBD = new Area_banco();

        try {
            while (areaBD.consulta.next()) {

                if (areaBD.consulta.getString("abreviacao").equals(areaSelecionada)) {
                    area.setNome(areaBD.consulta.getString("nome"));
                    area.setAbreviacao(areaBD.consulta.getString("abreviacao"));
                }
            }
        } catch (SQLException ex) {
            //JOptionPane.showMessageDialog(null, "erro ao criar area");

            System.out.println("erro ao criar objeto Area");
        }

//cadastrar funcionario
        novoFuncionario.setNome(jT_nome.getText());
        novoFuncionario.setCargo(cargo);
        novoFuncionario.setFormaDePagamento(pagamento);
        novoFuncionario.setCpf(jTF_cpf.getText());
        novoFuncionario.setArea(area);
        novoFuncionario.setValorHora(Integer.parseInt(jTvalorhr_tc.getText()));
        novoFuncionario.setConta(Integer.parseInt(jtfConta.getText()));
        novoFuncionario.setAgencia(Integer.parseInt(jtfAgencia.getText()));
        novoFuncionario.setBanco(jtfBanco.getText());

        funcionarioBD.inserir(novoFuncionario);

        atualizarTabela();

        //JOptionPane.showMessageDialog(null, "Funcionario inserido com sucesso!");
        System.out.println("func inserido");


    }//GEN-LAST:event_jBcadastrar_tc1ActionPerformed

    private void JB_voltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_voltarActionPerformed

        this.setVisible(false);

    }//GEN-LAST:event_JB_voltarActionPerformed

    private void jtfAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfAgenciaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfAgenciaActionPerformed

    private void listarFormaPagamento() {

        pagamentoBD = new FormaDePagamento_Banco();

        try {
            while (pagamentoBD.consulta.next()) {

                jcbFormasPagamentos.addItem(pagamentoBD.consulta.getString("nome"));

            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void listarCargos() {

        cargoBD = new Cargo_Banco();

        try {
            while (cargoBD.consulta.next()) {

                jcbCargos.addItem(cargoBD.consulta.getString("nome"));

            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void listarAreas() {

        areaBD = new Area_banco();

        try {
            while (areaBD.consulta.next()) {

                jcbAreas.addItem(areaBD.consulta.getString("abreviacao"));

            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void atualizarTabela() {

        funcionarioBD = new Funcionario_Banco();

        DefaultTableModel dtm = new DefaultTableModel(null, new String[]{"Nome", "Cargo", "Forma de Pag.", "CPF", "Área", "Valor da Hora", "Conta", "Ag", "Banco"});

        try {
            while (funcionarioBD.consulta.next()) {

                Object[] opcoes = {funcionarioBD.consulta.getString("nome"), funcionarioBD.consulta.getString("cargo"),
                    funcionarioBD.consulta.getString("formaDePagamento"), funcionarioBD.consulta.getString("cpf"),
                    funcionarioBD.consulta.getString("area"), funcionarioBD.consulta.getString("valorDaHora"),
                    funcionarioBD.consulta.getString("conta"), funcionarioBD.consulta.getString("agencia"),
                    funcionarioBD.consulta.getString("banco")};
                dtm.addRow(opcoes);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroFormaPagamento.class.getName()).log(Level.SEVERE, null, ex);
        }
        JT_Table.setModel(dtm);

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_voltar;
    private javax.swing.JTable JT_Table;
    private javax.swing.JLabel Jback;
    private javax.swing.JButton jB_editar1;
    private javax.swing.JButton jB_remover;
    private javax.swing.JButton jBcadastrar_tc1;
    private javax.swing.JLabel jLB_area;
    private javax.swing.JLabel jLB_cargo;
    private javax.swing.JLabel jLB_nome;
    private javax.swing.JLabel jLB_valorArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTF_cpf;
    private javax.swing.JTextField jT_nome;
    private javax.swing.JTextField jTvalorhr_tc;
    private javax.swing.JComboBox jcbAreas;
    private javax.swing.JComboBox jcbCargos;
    private javax.swing.JComboBox jcbFormasPagamentos;
    private javax.swing.JTextField jtfAgencia;
    private javax.swing.JTextField jtfBanco;
    private javax.swing.JTextField jtfConta;
    // End of variables declaration//GEN-END:variables
}
